package com.yj.rselasticsearch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yj.common.constant.DataConstants;
import com.yj.common.utils.FastUtils;
import com.yj.rselasticsearch.domain.dto.RetrievalRecordDto;
import com.yj.rselasticsearch.domain.vo.RetrievalRecordVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.yj.rselasticsearch.mapper.RetrievalRecordMapper;
import com.yj.common.core.domain.entity.RetrievalRecord;
import com.yj.rselasticsearch.service.RetrievalRecordService;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Service
public class RetrievalRecordServiceImpl extends ServiceImpl<RetrievalRecordMapper, RetrievalRecord> implements RetrievalRecordService {

    @Resource
    private RetrievalRecordMapper retrievalRecordMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return retrievalRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(RetrievalRecord record) {
        return retrievalRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(RetrievalRecord record) {
        return retrievalRecordMapper.insertSelective(record);
    }

    @Override
    public RetrievalRecord selectByPrimaryKey(Integer id) {
        return retrievalRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(RetrievalRecord record) {
        return retrievalRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(RetrievalRecord record) {
        return retrievalRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public IPage<RetrievalRecordVo> getRetrievalRecordList(RetrievalRecordDto retrievalRecordDto) {
        IPage<RetrievalRecordVo> retrievalRecordIPage = new Page<>(retrievalRecordDto.getPageIndex(), retrievalRecordDto.getPageSize());
        //获取列表信息
        List<RetrievalRecord> retrievalRecordList = list(new LambdaQueryWrapper<RetrievalRecord>().like(!FastUtils.checkNull(retrievalRecordDto.getKeyword()), RetrievalRecord::getKeyword, retrievalRecordDto.getKeyword()));
        if (!FastUtils.checkNullOrEmpty(retrievalRecordList)) {
            //分组求和
            Map<String, Long> result = retrievalRecordList.stream().collect(Collectors.groupingBy(RetrievalRecord::getKeyword, TreeMap::new, Collectors.counting()));
            List<RetrievalRecordVo> whiteListVos = new LinkedList<>();
            //奖分组信息赛回list
            result.forEach((k, v) -> {
                RetrievalRecordVo retrievalRecord = new RetrievalRecordVo();
                retrievalRecord.setDataCount(v);
                retrievalRecord.setKeyword(k);
                whiteListVos.add(retrievalRecord);
            });
            List<RetrievalRecordVo> whiteListVos1;
            //排序
            if (retrievalRecordDto.getOrderByMethod().equals(DataConstants.ORDERBYMETHOD.ASC)) {
                whiteListVos1 = whiteListVos.stream().sorted((o1, o2) -> {
                    if (o1.getDataCount() > o2.getDataCount()) {
                        return 1;
                    } else if (o1.getDataCount() < o2.getDataCount()) {
                        return -1;
                    } else {
                        return 1;
                    }
                }).collect(Collectors.toList());
            } else {
                whiteListVos1 = whiteListVos.stream().sorted((o1, o2) -> {
                    if (o1.getDataCount() > o2.getDataCount()) {
                        return -1;
                    } else if (o1.getDataCount() < o2.getDataCount()) {
                        return 1;
                    } else {
                        return 1;
                    }
                }).collect(Collectors.toList());
            }
            //手动分页
            int pages = whiteListVos.size() / retrievalRecordDto.getPageSize();
            if (whiteListVos.size() % retrievalRecordDto.getPageSize() > 0) {
                pages += 1;
                retrievalRecordIPage.setPages(pages);

            }
            int currIdx = (retrievalRecordDto.getPageIndex() > 1 ? (retrievalRecordDto.getPageIndex() - 1) * retrievalRecordDto.getPageSize() : 0);
            List<RetrievalRecordVo> whiteListVos2 = new LinkedList<>();
            for (int i = 0; i < retrievalRecordDto.getPageSize() && i < whiteListVos1.size() - currIdx; i++) {
                whiteListVos2.add(whiteListVos1.get(currIdx + i));
            }
            retrievalRecordIPage.setRecords(whiteListVos2);
            retrievalRecordIPage.setTotal(result.size());
        }
        return retrievalRecordIPage;
    }

}
